package main

import (
	"encoding/json"
	"fmt"
)

type Monster struct {
	Name     string  `json:"name"`
	Age      int     `json:"age"`
	Birthday string  `json:"birthday"`
	Salary   float64 `json:"salary"`
	Skill    string  `json:"skill"`
}

func unserialize() {
	str := "{\"name\":\"牛魔王\",\"age\":500,\"birthday\":\"1500\",\"salary\":50000,\"skill\":\"牛魔拳\"}"
	var monster Monster
	err := json.Unmarshal([]byte(str), &monster)
	if err != nil {
		fmt.Println("unmarshal failed")
	}
	fmt.Println(monster)
}

//反序列化成map
func unserializeMap() {
	str := "{\"name\":\"牛魔王\",\"age\":500,\"birthday\":\"1500\",\"salary\":50000,\"skill\":\"牛魔拳\"}"
	var a map[string]interface{}
	//反序列化
	err := json.Unmarshal([]byte(str), &a)
	if err != nil {
		fmt.Println("unmarshal failed")
	}
	fmt.Println(a)
}

//json反序列化成切片
func unserializeSlice() {
	str := "[{\"name\":\"vn\",\"skill\":[\"终极时刻\",\"闪避突袭\",\"无情猎手\"]},{\"name\":\"jinx\",\"skill\":[\"枪炮交响曲\",\"暴走快感\"]},{\"name\":\"riven\",\"skill\":\"折翼之舞\"}]"
	var slice []map[string]interface{}
	err := json.Unmarshal([]byte(str), &slice)
	if err != nil {
		fmt.Println("unmarshal failed")
	}
	fmt.Println(slice)
}
func main() {
	unserializeSlice()
}
